package controllers;

import is.ru.honn.rupin.domain.User;
import is.ru.honn.rupin.domain.UserAuthentication;
import is.ru.honn.rupin.service.UserService;
import play.data.Form;
import play.mvc.Result;


import views.html.index;
import views.html.login.*;
import views.html.session.form;
import views.html.session.home;


public class Login extends RuPinController
{
    final static Form<UserAuthentication> loginForm = form(UserAuthentication.class);

    public static Result blank()
    {
        return ok(form.render(loginForm));
    }

    public static Result submit()
    {
        Form<UserAuthentication> filledForm = loginForm.bindFromRequest();

        /*
        if (!"true".equals(filledForm.field("accept").value()))
        {
            filledForm.reject("accept", "You must accept the terms and conditions");
        }
        */

        UserService userService = (UserService)ctx.getBean("userService");

        User user = userService.getUser(filledForm.get().getUsername());

        if( !(userService.login(user.getUsername(),user.getPassword()) == null))
        {
            //Save user to session (username and password)
            session(filledForm.get().getUsername() , user.getEmail());
        }
        else
        {
            return badRequest(form.render(filledForm));
        }

        return ok(index.render(session().toString()));
    }

    public static Result boards()
    {
        return TODO;
    }

}



